package com.example.farmer.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.farmer.Database.ProductPublishDatabase;
import com.example.farmer.Database.PurchasePublishDatabase;
import com.example.farmer.Entity.ProductPost;
import com.example.farmer.Entity.PurchasePost;
import com.example.farmer.R;

import java.util.List;

public class MyPurchaseAdapter extends RecyclerView.Adapter<MyPurchaseAdapter.PurchaseViewHolder> {
    private static List<PurchasePost> purchasePosts;
    private static Context context;


    // 构造函数，接收帖子列表和上下文
    public MyPurchaseAdapter(List<PurchasePost> purchasePosts, Context context) {
        this.purchasePosts = purchasePosts;
        this.context = context;
    }

    @NonNull
    @Override
    public PurchaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 从布局文件中加载帖子项的视图
        View postView = LayoutInflater.from(parent.getContext()).inflate(R.layout.purchase_post_item, parent, false);
        return new PurchaseViewHolder(postView);
    }

    @Override
    public void onBindViewHolder(@NonNull PurchaseViewHolder holder, int position) {
        // 获取当前位置的帖子对象
        PurchasePost purchasePost = purchasePosts.get(position);
        // 绑定帖子内容和图片到ViewHolder
        holder.bind(purchasePost);
    }

    @Override
    public int getItemCount() {
        // 返回帖子列表的大小
        return purchasePosts == null ? 0 : purchasePosts.size();
    }

    // 更新帖子数据的方法
    public void updatePosts(List<PurchasePost> newPurchasePost) {
        if (newPurchasePost != null) {
            this.purchasePosts = newPurchasePost;
            notifyItemRemoved(1); // 通知数据集改变
        }
    }

    // ViewHolder内部类
    public class PurchaseViewHolder extends RecyclerView.ViewHolder {
        public TextView productname,producttype,need,period;
        public ImageView productimage;
        public TextView textDelete;
        public LinearLayout linearLayout;
        // 其他视图组件...




        public PurchaseViewHolder(View itemView) {
            super(itemView);
            // 绑定视图组件
            productimage = itemView.findViewById(R.id.productimage);
            productname = itemView.findViewById(R.id.productname);
            producttype = itemView.findViewById(R.id.producttype);
            need = itemView.findViewById(R.id.need);
            period = itemView.findViewById(R.id.period);
            textDelete = itemView.findViewById(R.id.text_delete);
            linearLayout = itemView.findViewById(R.id.class_post);
            // 其他视图组件初始化...

//            linearLayout.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    // 点击事件，跳转到WebViewActivity查看详情
//                    Intent intent = new Intent(context, News.class);
//                    context.startActivity(intent);
////                    Toast.makeText(context, "点击查看详情", Toast.LENGTH_SHORT).show();
//                }
//            });
            textDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 删除帖子的逻辑
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        PurchasePost purchasePostsToDelete = purchasePosts.get(position);
                        PurchasePublishDatabase purchasePublishDatabase = PurchasePublishDatabase.getInstance(context);
                        purchasePublishDatabase.deletePost(purchasePostsToDelete.getId());

                        // 从列表中移除帖子，并更新UI
                        purchasePosts.remove(position);
                        notifyItemRemoved(position);
                    }
                }
            });

        }

        // bind方法用于将帖子数据绑定到ViewHolder的视图上
        @SuppressLint("SetTextI18n")
        public void bind(PurchasePost purchasePost) {
            productname.setText("名称：" + purchasePost.getProductname());
            producttype.setText("类型：" + purchasePost.getProducttype());
            need.setText("需求量：" + purchasePost.getNeed());
            period.setText("采购周期：" + purchasePost.getPeriod());

            }
            // 绑定其他视图组件...
        }
    }